package vn.egame.elockscreen;

import vn.egame.elockscreen.activity.ConfirmPasscodeActivity;
import vn.egame.elockscreen.activity.SetPasscodeActivity;
import vn.egame.elockscreen.service.ScreenLockerService;
import vn.egame.elockscreen.utils.Fonts;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private SharedPreferences pre;
	int height;
	int width;

	ImageView btnActionBack, enable_lockscreen, enable_passcode;
	LinearLayout btnLockscreen, btnPin, btnChangePin;
	private TextView tvChangePin;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		if (!ScreenLockerService.isRunning) {

			// Calendar cal = Calendar.getInstance();
			//
			// Intent intent = new Intent(this, ScreenLockerService.class);
			// PendingIntent pintent = PendingIntent
			// .getService(this, 0, intent, 0);
			//
			// AlarmManager alarm = (AlarmManager)
			// getSystemService(Context.ALARM_SERVICE);
			// // Start every minute
			// alarm.setRepeating(AlarmManager.RTC_WAKEUP,
			// cal.getTimeInMillis(),
			// 1000, pintent);

			ScreenLockerService.startActionStart(this, null, null);

		}

		setContentView(R.layout.activity_main);
		
		String toastMsg = null;

		switch (getResources().getDisplayMetrics().densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			Log.d("Kai", "DENSITY_LOW");
			toastMsg = "Low";
			break;
		case DisplayMetrics.DENSITY_MEDIUM:
			Log.d("Kai", "DENSITY_MEDIUM");
			toastMsg = "medium";
			break;
		case DisplayMetrics.DENSITY_HIGH:
			Log.d("Kai", "DENSITY_HIGH");
			toastMsg = "Hight";
			break;
		case DisplayMetrics.DENSITY_XHIGH:
			Log.d("Kai", "DENSITY_XHIGH");
			toastMsg = "Xhight";
			break;
		}
		
//		Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
		
		int screenSize = getResources().getConfiguration().screenLayout &
		        Configuration.SCREENLAYOUT_SIZE_MASK;

		
		switch(screenSize) {
		    case Configuration.SCREENLAYOUT_SIZE_LARGE:
		        toastMsg = "Large screen";
		        break;
		    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
		        toastMsg = "Normal screen";
		        break;
		    case Configuration.SCREENLAYOUT_SIZE_SMALL:
		        toastMsg = "Small screen";
		        break;
		    default:
		        toastMsg = "Screen size is neither large, normal or small";
		}
//		Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();

		ELockscreenPreferences.getInstance().initPreferences(
				getApplicationContext());
		ELockscreenPreferences.getInstance().setLockerPackageTemplate("1");
		Fonts.getInstance().init(getApplicationContext());
		//
		// DisplayMetrics displaymetrics = new DisplayMetrics();
		// getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		// height = displaymetrics.heightPixels;
		// width = displaymetrics.widthPixels;
		//
		// // preview = (Preference) findPreference("preview");
		// // preview.setOnPreferenceClickListener(new
		// OnPreferenceClickListener() {
		// // public boolean onPreferenceClick(Preference preference) {
		// //
		// // getApplicationContext().startActivity(new
		// Intent(getApplicationContext(),
		// // LockScreenActivity.class).putExtra(
		// // Constans.START_SCREEN_SAVER_TYPE,
		// // Constans.StartScreenSaverType.SCREEN_OFF.getValue())
		// // .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
		// //
		// // return true;
		// // }
		// // });
		//
		// changePin = (Preference) findPreference("changePasscode");
		// changePin.setOnPreferenceClickListener(new
		// OnPreferenceClickListener() {
		// public boolean onPreferenceClick(Preference preference) {
		//
		// Intent intent = new Intent(getApplicationContext(),
		// ConfirmPasscodeActivity.class);
		// intent.putExtra("CONFIRM", "CHANGE_PIN");
		// startActivity(intent);
		//
		// return true;
		// }
		// });
		//

		btnActionBack = (ImageView) findViewById(R.id.btnBack);
		btnActionBack.setOnClickListener(onClickListener);

		enable_lockscreen = (ImageView) findViewById(R.id.cb_lockscreen);
		enable_lockscreen.setSelected(ELockscreenPreferences.getInstance()
				.isEnableLockscreen());

		enable_passcode = (ImageView) findViewById(R.id.cb_password);
		enable_passcode.setSelected(ELockscreenPreferences.getInstance()
				.isEnablePasscode());

		btnLockscreen = (LinearLayout) findViewById(R.id.btnLockscreen);
		btnLockscreen.setOnClickListener(onClickListener);

		btnPin = (LinearLayout) findViewById(R.id.btnPin);
		btnPin.setOnClickListener(onClickListener);

		btnChangePin = (LinearLayout) findViewById(R.id.btnChangePin);
		btnChangePin.setOnClickListener(onClickListener);

		tvChangePin = (TextView) findViewById(R.id.tv_change_pin);
		if (!ELockscreenPreferences.getInstance().isEnablePasscode()) {
			btnChangePin.setClickable(false);
			tvChangePin.setTextColor(getResources().getColor(R.color.smoke));
		}
	}

	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub

		}
	};

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btnChangePin) {
				if (ELockscreenPreferences.getInstance().isEnablePasscode()) {
					Intent intent = new Intent(getApplicationContext(),
							ConfirmPasscodeActivity.class);
					intent.putExtra("CONFIRM", "CHANGE_PIN");
					startActivity(intent);
				}
			} else if (v.getId() == R.id.btnPin) {
				if (ELockscreenPreferences.getInstance().isEnablePasscode()) {
					Intent intent = new Intent(getApplicationContext(),
							ConfirmPasscodeActivity.class);
					intent.putExtra("CONFIRM", "DISABLE_PIN");
					startActivity(intent);

				} else {
					Intent intent = new Intent(getApplicationContext(),
							SetPasscodeActivity.class);
					startActivity(intent);

				}

			} else if (v.getId() == R.id.btnLockscreen) {
				ELockscreenPreferences.getInstance().setEnableLockscreen(
						!ELockscreenPreferences.getInstance()
								.isEnableLockscreen());

				enable_lockscreen.setSelected(ELockscreenPreferences
						.getInstance().isEnableLockscreen());
			} else if (v.getId() == R.id.btnBack) {

				// String SettingsPage =
				// "com.android.settings/.lockscreen.ChooseLockPattern";
				//
				//
				// Intent intent = new Intent(Intent.ACTION_MAIN);
				// intent.setComponent(ComponentName
				// .unflattenFromString(SettingsPage));
				// intent.addCategory(Intent.CATEGORY_LAUNCHER);
				// startActivity(intent);

				finish();

				// final Intent intent = new Intent(Intent.ACTION_MAIN, null);

				// ComponentName cn = new
				// ComponentName("com.android.settings",
				// "com.android.settings.bluetoothSettings");

				// ComponentName cn = new ComponentName("com.android.settings",
				// "com.android.settings.ChooseLockGeneric");
				// intent.setComponent(cn);
				// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				// startActivity(intent);

				// startActivity(new
				// Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
			}
		}
	};

	private void launchComponent(String packageName, String name) {
		Intent launch_intent = new Intent("android.intent.action.MAIN");
		launch_intent.addCategory("android.intent.category.LAUNCHER");
		launch_intent.setComponent(new ComponentName(packageName, name));
		launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		startActivity(launch_intent);
	}

	private void openUrl(String url) {
		try {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
		} catch (Exception e) {

		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		// Log.d("Kai", "Resume: " +
		// S5KeypadPreferences.getInstance().isEnablePasscode());

		enable_passcode.setSelected(ELockscreenPreferences.getInstance()
				.isEnablePasscode());

		if (!ELockscreenPreferences.getInstance().isEnablePasscode()) {
			btnChangePin.setClickable(false);
			tvChangePin.setTextColor(getResources().getColor(R.color.smoke));
		} else {
			btnChangePin.setClickable(true);
			tvChangePin.setTextColor(getResources().getColor(R.color.lynch));
		}

		// if (ELockscreenPreferences.getInstance().isEnablePasscode()) {
		// en.setEnabled(true);
		// } else {
		// changePin.setEnabled(false);
		// }
		super.onResume();
	}
}
